/**    
* @文件名: LogServiceImpl.java  
* @包名 cn.com.gzheroli.core.common.service.impl  
* @描述: TODO(用一句话描述该文件做什么)  
* @作者 李长荣  lichang_20011@163.com   
* @日期 2016年10月29日 下午11:42:13  
* @version V1.0    
*/
package cn.com.gzheroli.rabbitmq.service.log;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.gzheroli.bfw.pojo.base.TSLog;
import cn.com.gzheroli.bfw.pojo.base.TSLogFlowcfg;
import cn.com.gzheroli.bfw.pojo.base.TSLogLogin;
import cn.com.gzheroli.bfw.pojo.base.TSLogRoleauth;
import cn.com.gzheroli.bfw.pojo.base.TSUser;
import cn.com.gzheroli.cache.SpringRedisCache;
import cn.com.gzheroli.core.common.mapper.CommonMapper;
import cn.com.gzheroli.core.common.mapper.LogMapper;
import cn.com.gzheroli.core.manager.Client;
import cn.com.gzheroli.core.util.BrowserUtils;
import cn.com.gzheroli.core.util.ContextHolderUtils;
import cn.com.gzheroli.core.util.DataUtils;
import cn.com.gzheroli.core.util.IdWorker;
import cn.com.gzheroli.core.util.oConvertUtils;
import cn.com.gzheroli.rabbitmq.service.producer.MessageProducerService;
import cn.com.gzheroli.rabbitmq.service.log.LogService;

/**  
* @类名: LogServiceImpl  
* @描述: TODO(这里用一句话描述这个类的作用)  
* @作者: 李长荣 lichang_20011@163.com  
* @日期: 2016年10月29日 下午11:42:13  
*    
*/
@Service("logService")
public class LogServiceImpl implements LogService{
	@Autowired
	private LogMapper logMapper;
	@Autowired
    private SpringRedisCache springRedisCache;
	@Autowired
	MessageProducerService messageProducerService;
	//SQL 使用JdbcDao
	
	@Resource
	private CommonMapper commonMapper;
	
	/**
	 * 添加日志
	 */
	public void addLog(String logcontent, Short loglevel, Short operatetype,Map logMap) {
		//HttpServletRequest request = ContextHolderUtils.getRequest();
		String broswer = (String)logMap.get("broswer");
		String ip = (String)logMap.get("ip");
		String token =logMap.get("token")==null? (String)logMap.get("sessionId"):logMap.get("token").toString();
		TSLog log = new TSLog();
		IdWorker idWorker = IdWorker.getFlowIdWorkerInstance();
		log.setId(idWorker.nextIdStr());
		
		log.setLogContent(logcontent);
		log.setLogLevel(loglevel);
		log.setOperateType(operatetype);
		log.setNote(ip);
		log.setBroswer(broswer);
		log.setOperateTime(DataUtils.gettimestamp());
		Client client=springRedisCache.getCache(token, Client.class);
		if(client!=null)
			log.setUserId(client.getUser().getUserName());
		//mqFanoutService.sendMessage(log,"loginsertJob");
		//commonDao.save(log);
		//jdbcDao.insert(log);
		
		messageProducerService.sendMessage(log);
	}
	
	public void addFlowcfgLog(String logContent,Short operatetype,String opId,Map logMap) {
		// TODO Auto-generated method stub
		//HttpServletRequest request = ContextHolderUtils.getRequest();
		//String broswer = BrowserUtils.checkBrowse(request);
		String broswer = (String)logMap.get("broswer");
		String ip = (String)logMap.get("ip");
		String token =logMap.get("token")==null? (String)logMap.get("sessionId"):logMap.get("token").toString();
		TSLogFlowcfg log = new TSLogFlowcfg();
		IdWorker idWorker = IdWorker.getFlowIdWorkerInstance();
		log.setId(idWorker.nextIdStr());
		log.setLogContent(logContent);
		log.setOperateType(operatetype);
		log.setIpAddr(oConvertUtils.getIp());
		log.setBroswer(broswer);
		log.setOperateTime(DataUtils.gettimestamp());
		Client client=springRedisCache.getCache(token, Client.class);
		if(client!=null){
			log.setUserId(client.getUser().getUserName());
			log.setUserName(client.getUser().getRealName());
		}
			
		//log.setTSUser(redisCacheService.getSessionUserName(sessionId));
		
		log.setOpId(opId);
		//jdbcDao.insert(log);
		//Map<String,Object> paraMap=MyBatisSqlUtils.buildInsertSql(log, new DefaultNameHandler());
		//commonMapper.addRecord(paraMap);
		messageProducerService.sendMessage(log);
	}

	/**
	 * 把主日志拆分 到其他日志
	 * @param log
	 */
	private void addToOtherLog(TSLog log,Map logMap){
		String type= log.getLogContent().substring(0,2);
		if("用户".equals(type))
		{
			addLogLogin(log.getLogContent(),log.getOperateType(), null,logMap);
		}else if("权限".equals(type)){
			addLogRoleauth(log.getLogContent(),log.getOperateType(), null,logMap);
		}else{
			//jdbcDao.insert(log);// 以后 业务Log 保存地方
			//Map<String,Object> paraMap=MyBatisSqlUtils.buildInsertSql(log, new DefaultNameHandler());
			//commonMapper.addRecord(paraMap);
			messageProducerService.sendMessage(log);
		}
		
	}
	
	public void addLogLogin(String logContent,Short operatetype,Map otherInfo,Map logMap) {
		//HttpServletRequest request = ContextHolderUtils.getRequest();
		//String broswer = BrowserUtils.checkBrowse(request);
		String broswer = (String)logMap.get("broswer");
		String ip = (String)logMap.get("ip");
		String token =logMap.get("token")==null? (String)logMap.get("sessionId"):logMap.get("token").toString();
		if("OTHER".equals(broswer)&&otherInfo!=null&&otherInfo.containsKey("apptype"))
			broswer=otherInfo.get("apptype").toString();  
		TSLogLogin log=new TSLogLogin();
		IdWorker idWorker = IdWorker.getFlowIdWorkerInstance();
		log.setId(idWorker.nextIdStr());
		log.setLogContent(logContent);
		log.setOperateType(operatetype);
		log.setIpAddr(oConvertUtils.getIp());
		log.setBroswer(broswer);
		log.setOperateTime(DataUtils.gettimestamp());
		
		Client client=springRedisCache.getCache(token, Client.class);
		if(client!=null){
			log.setUserId(client.getUser().getUserName());
			log.setUserName(client.getUser().getRealName());
		}
		if(otherInfo!=null&&otherInfo.containsKey("status"))
			log.setStatus(otherInfo.get("status").toString());
		else 
			log.setStatus("1");
		
		//jdbcDao.insert(log);
		//Map<String,Object> paraMap=MyBatisSqlUtils.buildInsertSql(log, new DefaultNameHandler());
		//commonMapper.addRecord(paraMap);
		messageProducerService.sendMessage(log);
		
	}

	public void addLogRoleauth(String logContent,Short operatetype,Map otherInfo,Map logMap) {
		//HttpServletRequest request = ContextHolderUtils.getRequest();
		//String broswer = BrowserUtils.checkBrowse(request);
		String broswer = (String)logMap.get("broswer");
		String ip = (String)logMap.get("ip");
		String token =logMap.get("token")==null? (String)logMap.get("sessionId"):logMap.get("token").toString();
		if("OTHER".equals(broswer)&&otherInfo!=null&&otherInfo.containsKey("apptype"))
			broswer=otherInfo.get("apptype").toString();  
		TSLogRoleauth log=new TSLogRoleauth();
		IdWorker idWorker = IdWorker.getFlowIdWorkerInstance();
		log.setId(idWorker.nextIdStr());
		log.setLogContent(logContent);
		log.setOperateType(operatetype);
		log.setIpAddr(oConvertUtils.getIp());
		log.setBroswer(broswer);
		log.setOperateTime(DataUtils.gettimestamp());
		Client client=springRedisCache.getCache(token, Client.class);
		if(client!=null){
			log.setUserId(client.getUser().getUserName());
			log.setUserName(client.getUser().getRealName());
		}
		//jdbcDao.insert(log);
		//Map<String,Object> paraMap=MyBatisSqlUtils.buildInsertSql(log, new DefaultNameHandler());
		//commonMapper.addRecord(paraMap);
		messageProducerService.sendMessage(log);
	}

	public List<Map<String, String>> getLogLoginfList(HttpServletRequest request) {
		Integer page = Integer.valueOf(Integer.parseInt(request.getParameter("page")));
	    Integer rows = Integer.valueOf(Integer.parseInt(request.getParameter("rows")));
	    Integer start = Integer.valueOf((page.intValue() - 1) * rows.intValue())+1;
	    Integer end = Integer.valueOf(page.intValue() * rows.intValue() );
	    HashMap parameter=new HashMap();
	    String userid=request.getParameter("userid");
	    String logcontent=request.getParameter("logcontent");
	    String ipaddr=request.getParameter("ipaddr");
	    String broswer=request.getParameter("broswer");

	    parameter.put("userid",userid);
	    parameter.put("logcontent", logcontent);
	    parameter.put("ipaddr", ipaddr);
	    parameter.put("broswer", broswer);
	    parameter.put("maxResults", end.intValue());
		parameter.put("firstResult", start.intValue());
	    List list=logMapper.getLogLoginfList(parameter);
	    return list;
	}

	public Long getLogLoginfListAcount(HttpServletRequest request) {
		 HashMap parameter=new HashMap();
		    String userid=request.getParameter("userid");
		    String logcontent=request.getParameter("logcontent");
		    String ipaddr=request.getParameter("ipaddr");
		    String broswer=request.getParameter("broswer");

		    parameter.put("userid",userid);
		    parameter.put("logcontent", logcontent);
		    parameter.put("ipaddr", ipaddr);
		    parameter.put("broswer", broswer);

		return logMapper.getLogLoginfListAcount(parameter);
	}

	public List<Map<String, String>> getLogRoleauthfList(
			HttpServletRequest request) {
		Integer page = Integer.valueOf(Integer.parseInt(request.getParameter("page")));
	    Integer rows = Integer.valueOf(Integer.parseInt(request.getParameter("rows")));
	    Integer start = Integer.valueOf((page.intValue() - 1) * rows.intValue())+1;
	    Integer end = Integer.valueOf(page.intValue() * rows.intValue() );
	    HashMap parameter=new HashMap();
	    String userid=request.getParameter("userid");
	    String logcontent=request.getParameter("logcontent");
	    String ipaddr=request.getParameter("ipaddr");
	    String broswer=request.getParameter("broswer");

	    parameter.put("userid",userid);
	    parameter.put("logcontent", logcontent);
	    parameter.put("ipaddr", ipaddr);
	    parameter.put("broswer", broswer);
	    parameter.put("maxResults", end.intValue());
		parameter.put("firstResult", start.intValue());
	    List list=logMapper.getLogRoleauthfList(parameter);
	    return list;
	}

	public Long getLogRoleauthListAcount(HttpServletRequest request) {
		HashMap parameter=new HashMap();
	    String userid=request.getParameter("userid");
	    String logcontent=request.getParameter("logcontent");
	    String ipaddr=request.getParameter("ipaddr");
	    String broswer=request.getParameter("broswer");

	    parameter.put("userid",userid);
	    parameter.put("logcontent", logcontent);
	    parameter.put("ipaddr", ipaddr);
	    parameter.put("broswer", broswer);

	return logMapper.getLogRoleauthListAcount(parameter);
	}
}
